package com.smarttest.common.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by zachary on 2016/10/13.
 */
public class Md5Util {

    public Md5Util() {
    }

    public static byte[] computeMD5Hash(InputStream is) throws NoSuchAlgorithmException,
            IOException {
        BufferedInputStream bis = new BufferedInputStream(is);

        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[16384];
            boolean bytesRead = true;

            int bytesRead1;
            while ((bytesRead1 = bis.read(buffer, 0, buffer.length)) != -1) {
                messageDigest.update(buffer, 0, bytesRead1);
            }

            byte[] var5 = messageDigest.digest();
            return var5;
        } finally {
            try {
                bis.close();
            } catch (Exception var12) {
                System.err.println("Unable to close input stream of hash candidate: " + var12);
            }

        }
    }

    public static byte[] computeMD5Hash(byte[] data) throws NoSuchAlgorithmException, IOException {
        return computeMD5Hash((InputStream) (new ByteArrayInputStream(data)));
    }
}